/**
 * Created by moc on 2016/3/30.
 */

jQuery(document).ready(function ($) {

    $('#btnNewNote').click(function () {
        var title = $('#formNewNote #title').val()
        var content = $('#formNewNote #content').val()
        $.ajax({
            url: "/b/newnote",
            type: 'POST',
            data: {title: title, content: content},
        })
            .done(function (res) {
                if (res.code == 0) {
                    location.href="/"
                } else {
                    console.log(res)
                }
            })
    })

    $('#btnLogout').click(function(){
        $.ajax({
            url: "/b/logout",
            type: 'POST',
            data: {},
        })
            .done(function (res) {
                if (res.code == 0) {
                    location.href="/login"
                } else {
                    console.log(res)
                }
            })
    })

    $('#btnLogin').click(function(){
        var name=$('#formLogin #username').val()
        var pw=$('#formLogin #password').val()
        console.log(name,pw)
        $.ajax({
            url: "/b/login",
            type: 'POST',
            data: {username:name,password:pw},
        })
            .done(function (res) {
                if (res.code == 0) {
                    location.href="/"
                } else {
                    console.log(res)
                }
            })
    })

    $('#btnRegister').click(function(){
        var name=$('#formRegister #username').val()
        var pw=$('#formRegister #password').val()
        $.ajax({
            url: "/b/register",
            type: 'POST',
            data: {username:name,password:pw},
        })
            .done(function (res) {
                if (res.code == 0) {
                    window.alert("注册成功")
                    location.href="/login"
                } else {
                    window.alert(res.msg)
                    console.log(res)
                }
            })
    })
})